home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / rbsetnv1.zip / CHGPATH.C < prev    next >
Text File  |  1990-04-13  |  1KB  |  63 lines

  1. /*
  2.  * chgpath.c
  3.  *
  4.  * Find the PATH variable, search for <pathname1> and if found, replace by
  5.  * <pathname2>.  In any event, leave pathname2 in the path.
  6.  *
  7.  * Returned status:
  8.  *    0    OK (including case where nothing needs to be done)
  9.  *    1    bad arguments
  10.  *
  11.  */
  12. #include <io.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15.  
  16. void exit(int status) { _exit(status); }
  17.  
  18. #define writes(s,i)  write(i,s,strlen(s));
  19.  
  20. main(int argc, char *argv[])
  21. {
  22.     int len;
  23.     char *path, *pathname1, *pathname2, *str;
  24.  
  25.     if( argc != 3)    {
  26.         writes("Usage: inspath  path_to_replace  new_path\n",2);
  27.         exit(1);
  28.     } else    {
  29.         len = strlen(argv[1]);
  30.         if(*(argv[1]+len-1) == ';') *(argv[1]+len-1) = '\0';
  31.         len = strlen(argv[2]);
  32.         if(*(argv[2]+len-1) == ';') *(argv[1]+len-1) = '\0';
  33.         pathname1 = strupr(argv[1]);
  34.         pathname2 = strupr(argv[2]);
  35.         path  = getenv("PATH");
  36.  
  37.         len = strlen(pathname1);
  38.         str = strupr(path);
  39.         while (str)    {
  40.             str = strstr(str, pathname1);
  41.             if(!str)    {
  42.                 /* pathname1 not found - just tag pathname2 on the end */
  43.                 writes(path,1);
  44.                 writes(";",1)
  45.                 writes(pathname2,1);
  46.                 break;
  47.             } else if (*(str+len) == ';' || *(str+len) == '\0')    {
  48.                 /* found it - truncate before pathname1 */
  49.                 *str = '\0';
  50.                 writes(path,1);
  51.                 writes(pathname2,1);
  52.                 writes(str+len,1);
  53.                 break;
  54.             } else    {
  55.                 /* found an incorrect match - keep looking */
  56.                 str += len;
  57.             }
  58.         }
  59.     }
  60.     return(0);
  61. }
  62.  
  63.